﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Fcom.VNInsurance.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
              "DeleteCPID", // Route name
              "CopanyProduct/DeleteCPID/{ID}", // URL with parameters
              new { controller = "CompanyProduct", action = "DeleteCPID" } // Parameter defaults
              , new { ID = @"\d+" }
              );

            //Moto/motobikedetail/13
            routes.MapRoute(
               "motobikedetail", // Route name
               "Vihicle/Motobike/Detail/{ID}", // URL with parameters
               new { controller = "Moto", action = "motobikedetail" } // Parameter defaults
               , new { ID = @"\d+" }
               );

            //sitemap
            //benefitCPremove(int BenefitID,int CompanyProductID)
            routes.MapRoute(
                "benefitCPremove", // Route name
                "CompanyProduct/benefitCPremove/{BenefitID}/{CompanyProductID}", // URL with parameters
                new { controller = "CompanyProduct", action = "benefitCPremove" } // Parameter defaults
                , new { BenefitID = @"\d+", CompanyProductID = @"\d+"}
                );

            //CompanyProduct/EditBenefitFare/?comapnyid=1&benefitid=1int companyproduct
            routes.MapRoute(
"CompanyProducteditbenefitfare", // Route name
"CompanyProduct/EditBenefitFare/{companyid}/{benefitid}/{companyproduct}", // URL with parameters
new { controller = "CompanyProduct", action = "EditBenefitFare" } // Parameter defaults
, new { companyid = @"\d+", benefitid = @"\d+", companyproduct = @"\d+" }
);

            routes.MapRoute(
            "sitemap", // Route name
            "sitemap.xml", // URL with parameters
            new { controller = "Home", action = "SiteMap" }); // Parameter defaults
            //Product/ProductCatList?CatID=1&CatName=Life
            //http://localhost:1884/Moto/MotobikeCompare
            //CompanyProduct/CompanyProductDetail/6
            //GroupHealthCare
            //("Compare", "CompanyProduct", new { ID=LangProduct.ProductID,Name=Fcom.VNInsurance.Web.Utils.Url.url_rewrite(LangProduct.DisplayName),CatName=Fcom.VNInsurance.Web.Utils.Url.url_rewrite(LangCat.DisplayName)})
            //
            //removeHoptialCoverage(int HopitalID, int CoverageID)
            routes.MapRoute(
"removeHoptialCoverage", // Route name
"removeHoptialCoverage/{HopitalID}/{CoverageID}", // URL with parameters
new { controller = "removeHoptialCoverage", action = "Coverage" } // Parameter defaults
, new { HopitalID = @"\d+", CoverageID=@"\d+" }
);
            routes.MapRoute(
"Comparecompanyproduct", // Route name
"compare/{CatName}/{Name}/{ID}/{package}", // URL with parameters
new { controller = "CompanyProduct", action = "Compare" } // Parameter defaults
, new { ID = @"\d+", package = @"\d+" }
);
   
            routes.MapRoute(
"CompanyProductDetail", // Route name
"detail/{CatName}/{ComName}/{Name}/{ID}", // URL with parameters
new { controller = "CompanyProduct", action = "CompanyProductDetail" } // Parameter defaults
, new { ID = @"\d+" }
);
            routes.MapRoute(
"MotobikeApply", // Route name
"Vehicle/Motobike/Apply/{ID}", // URL with parameters
new { controller = "Moto", action = "MotobikeApply" } // Parameter defaults
, new { ID = @"\d+" }
 );
            routes.MapRoute(
    "MotobikeCompare", // Route name
    "Vehicle/Motobike/Compare", // URL with parameters
    new { controller = "Moto", action = "MotobikeCompare" } // Parameter defaults
     );
            routes.MapRoute(
"Motobike", // Route name
"Vehicle/Motobike", // URL with parameters
new { controller = "Moto", action = "Motobike" } // Parameter defaults
);
            routes.MapRoute(
 "CarApply", // Route name
 "Vehicle/Car/Apply/{ID}", // URL with parameters
 new { controller = "Moto", action = "CarApply" } // Parameter defaults
 , new { ID = @"\d+" }
  );
            routes.MapRoute(
    "CarCompare", // Route name
    "Vehicle/Car/Compare", // URL with parameters
    new { controller = "Moto", action = "CarCompare" } // Parameter defaults
     );
            routes.MapRoute(
"Car", // Route name
"Vehicle/Car", // URL with parameters
new { controller = "Moto", action = "Car" } // Parameter defaults
);
           
            routes.MapRoute(
     "Applylives", // Route name
     "Life/Lives/Apply/{ID}", // URL with parameters
     new { controller = "Life", action = "LivesApply" } // Parameter defaults
     , new { ID=@"\d+"}
      );
            routes.MapRoute(
      "CompareLives", // Route name
      "Life/Lives/Compare", // URL with parameters
      new { controller = "Life", action = "CompareLives" } // Parameter defaults
   
       );
            routes.MapRoute(
       "EditCompany", // Route name
       "Company/EditCompany/{CompanyID}", // URL with parameters
       new { controller = "Company", action = "EditCompany" } // Parameter defaults
       , new { CompanyID=@"\d+"}
        );
            routes.MapRoute(
        "cpanel", // Route name
        "cpanel", // URL with parameters
        new { controller = "Home", action = "Cpanel" } // Parameter defaults
   
         );
            routes.MapRoute(
        "ProductCatList", // Route name
        "ProductCatelog/{CatName}/{CatID}", // URL with parameters
        new { controller = "Product", action = "ProductCatList" } // Parameter defaults
        , new { CatID = @"\d+" }
         );
            routes.MapRoute(
        "ajDeleteFaq", // Route name
        "Product/ajDeleteFaq/{FaqIDd}", // URL with parameters
        new { controller = "Product", action = "ajDeleteFaq" } // Parameter defaults
        , new { FaqIDd = @"\d+" }
         );
          
            //Product/ajAddFaq
         
            routes.MapRoute(
          "MNFaq", // Route name
          "Product/MNFaq/{ProductID}", // URL with parameters
          new { controller = "Product", action = "MNFaq" } // Parameter defaults
          , new { ProductID = @"\d+" }
           );
            routes.MapRoute(
          "MNProductDetail", // Route name
          "Product/MNProductDetail/{ProductID}", // URL with parameters
          new { controller = "Product", action = "MNProductDetail" } // Parameter defaults
          , new { ProductID = @"\d+" }
           );
            routes.MapRoute(
           "MNCatDetail", // Route name
           "Product/MNCatDetail/{CatID}", // URL with parameters
           new { controller = "Product", action = "MNCatDetail" } // Parameter defaults
           , new { CatID = @"\d+" }
            );

            routes.MapRoute(
           "Deletenews", // Route name
           "News/DeleteNewsByID/{NewsID}", // URL with parameters
           new { controller = "News", action = "DeleteNewsByID" } // Parameter defaults
            );
            routes.MapRoute(
            "newsmanagecat", // Route name
            "News/ManageCat/{CatID}", // URL with parameters
            new { controller = "News", action = "ManageCat" } // Parameter defaults
             );
            //News/EditNews/4
                routes.MapRoute(
             "newsedit", // Route name
             "News/EditNews/{NewsID}", // URL with parameters
             new { controller = "News", action = "EditNews" } // Parameter defaults
              );
         
            //end-sitemap
                //Index","Home"
                routes.MapRoute(
              "complaint", // Route name
              "vnbaohiem/complaint", // URL with parameters
              new { controller = "Home", action = "Complaint" } // Parameter defaults
               );
                //Index","Home"
                routes.MapRoute(
              "Career", // Route name
              "vnbaohiem/career", // URL with parameters
              new { controller = "Home", action = "Career" } // Parameter defaults
               );
            //Index","Home"
            routes.MapRoute(
          "contact", // Route name
          "vnbaohiem/contact", // URL with parameters
          new { controller = "Home", action = "Contact" } // Parameter defaults
           );
            //Index","Home"
            routes.MapRoute(
          "term", // Route name
          "vnbaohiem/policy-and-term", // URL with parameters
          new { controller = "Home", action = "Term" } // Parameter defaults
           );
            //home/about
            routes.MapRoute(
           "About", // Route name
           "vnbaohiem/about", // URL with parameters
           new { controller = "Home", action = "About" } // Parameter defaults
            );
            //?cpID=3&title=Bảo-hiểm-sinh-mạng-EN
            routes.MapRoute(
            "policy", // Route name
            "product/{title}/policy/cp{cpID}", // URL with parameters
            new { controller = "Product", action = "Policy" } // Parameter defaults
            , new { cpID = @"\d+" });
         
            //Compare
            routes.MapRoute(
              "compare", // Route name
              "product/{title}/compare/p{pID}", // URL with parameters
              new { controller = "Product", action = "Compare" } // Parameter defaults
              , new { pID = @"\d+" });
         
            routes.MapRoute(
              "producten", // Route name
              "product/{title}/p{pID}", // URL with parameters
              new { controller = "Product", action = "ProductDetail", id = UrlParameter.Optional } // Parameter defaults
              , new { pID = @"\d+" });
         
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            // Use LocalDB for Entity Framework by default
          //  Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (!Request.Url.Host.StartsWith("www") && !Request.Url.IsLoopback)
            {
                UriBuilder builder = new UriBuilder(Request.Url);
                builder.Host = "www." + Request.Url.Host;
                Response.StatusCode = 301;
                Response.AddHeader("Location", builder.ToString());
                Response.End();
            }
        }
    }
}